<!--
 * @Author: xws 1043495328@qq.com
 * @Date: 2023-07-17 17:56:27
 * @LastEditors: xws 1043495328@qq.com
 * @LastEditTime: 2023-07-31 11:24:11
 * @FilePath: \vue3-admin-template\src\views\acl\permission\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <Category :scene="scene" />
  <el-card style="margin: 10px 0px">
    <div v-show="scene == 0">
      <el-button
        size="default"
        type="primary"
        icon="Plus"
        :disabled="categoryStore.c3Id ? false : true"
        @click="addSpu"
      >
        添加SPU
      </el-button>
      <!-- 展示已有的spu数据 -->
      <el-table style="margin: 10px 0px" border :data="records">
        <el-table-column
          label="序号"
          type="index"
          align="center"
          width="80px"
        ></el-table-column>
        <el-table-column label="SPU名称" prop="spuName"></el-table-column>
        <el-table-column
          label="SPU描述"
          show-overflow-tooltip
          prop="description"
        ></el-table-column>
        <el-table-column label="SPU操作">
          <template #="{ row, $index }">
            <el-button
              size="default"
              type="success"
              icon="Plus"
              title="添加SKU"
              @click="addSku(row)"
            ></el-button>
            <el-button
              size="default"
              type="primary"
              icon="Edit"
              title="修改SPU"
              @click="updateSpu(row)"
            ></el-button>
            <el-button
              size="default"
              type="info"
              icon="View"
              title="查看SPU列表"
              @click="findSku(row)"
            ></el-button>
            <el-popconfirm
              :title="`你确定删除${row.spuName}?`"
              width="200px"
              @confirm="deleteSpu(row)"
            >
              <template #reference>
                <el-button
                  type="primary"
                  size="small"
                  icon="Delete"
                  title="删除SPU"
                ></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        :page-sizes="[3, 5, 7, 9]"
        :background="true"
        layout="  prev, pager, next, jumper,->,sizes,total"
        :total="total"
        @current-change="getHasSpu"
        @size-change="changeSize"
      />
    </div>
    <!-- 添加SPU|修改SPU子组件 -->
    <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spu" />
    <!-- 添加SKU的子组件 -->
    <SkuForm v-show="scene == 2" @changeScene="changeScene" ref="sku" />
    <!-- dialogo对话框:展示已有的sku数据 -->
    <el-dialog v-model="show" title="SKU列表">
      <el-table border :data="skuArr">
        <el-table-column label="SKU名字" prop="skuName"></el-table-column>
        <el-table-column label="SKU价格" prop="price"></el-table-column>
        <el-table-column label="SKU重量" prop="weight"></el-table-column>
        <el-table-column label="SKU图片">
          <template #="{ row, $index }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
import { onBeforeUnmount, ref, watch } from 'vue'
import useCategoryStore from '@/store/modules/category'
import { reqHasSpu, reqRemoveSpu, reqSkuList } from '@/api/product/spu'
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'
import type {
  HasSpuResponseData,
  Records,
  SkuInfoData,
  SpuData,
} from '@/api/product/spu/type'
import { ElMessage } from 'element-plus'
const categoryStore = useCategoryStore()
// 存储全部sku数组
const skuArr = ref<any>([])
//场景数据
const scene = ref<number>(0) //0:显示已有SPU 1:添加或者修改已有SPU 2:添加SKU的结构
// 分页器页码
const pageNo = ref<number>(1)
// 每页展示数据条数
const pageSize = ref<number>(3)
// 存储已有的SPU数据
const records = ref<Records>([])
// 存储sup总个数
const total = ref<number>()
// 获取子组件实例
const spu = ref<any>()
const sku = ref<any>()
const show = ref<boolean>(false)
//监听三级分类ID变化
watch(
  () => categoryStore.c3Id,
  () => {
    // 务必要有三级分类ID
    if (!categoryStore.c3Id) return
    getHasSpu()
  },
)
//此方法执行:可以获取某一个三级分类下全部的己有的SPU
const getHasSpu = async (pager = 1) => {
  // 修改当前页码
  pageNo.value = pager
  const result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categoryStore.c3Id,
  )
  if (result.code == 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}
//分页器的下拉菜单发生变化的时候触发
const changeSize = () => {
  getHasSpu()
}

const addSpu = () => {
  scene.value = 1
  spu.value.initAddSpu(categoryStore.c3Id)
}
// 修改SPU回调按钮
const updateSpu = (row: SpuData) => {
  scene.value = 1
  // 调用子组件实例方法，获取完整的spu数据
  spu.value.initHasSpuData(row)
}
const findSku = async (row: SpuData) => {
  const result: SkuInfoData = await reqSkuList(row.id as number)
  if (result.code == 200) {
    skuArr.value = result.data
    show.value = true
  }
}
//删除已有的SPU按钮的回调
const deleteSpu = async (row: SpuData) => {
  let result: any = await reqRemoveSpu(row.id as number)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    //获取剩余SPU数据
    getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
//路由组件销毁前，情况仓库关于分类的数据
onBeforeUnmount(() => {
    categoryStore.$reset();
})
//子组件SpuForm绑定自定义事件:目前是让子组件通知父组件切换场景为0
const changeScene = (obj: any) => {
  scene.value = obj.flag
  if (obj.params === 'update') {
    getHasSpu(pageNo.value)
  } else {
    getHasSpu()
  }
}
const addSku = (row: SpuData) => {
  scene.value = 2
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}
</script>

<style scoped></style>
